let app = getApp();
Page({
  data: {
    goods_list: [],
    zh: true,//综合排序
    zx: false,//最新排序
    zr: false,//最热排序
    off: false,
    def: true,
    up: false,
    down: false,
    act: '',
    actorder: '',
    keyword: '',
    sort_text: '',
    search_show: false,
    show: 0,
    total: '',
    firstRow: 0,
    is_show: 0,
    totalsize:0,
    shopname:'',
    is_show_time:1,
    user_id:'',
    shop_wap_qrcode:'',
    
  },
  onLoad: function (options) {
    var that = this
    var uid = options.uid
    var uu_id = options.uu_id
    if (uu_id){
      wx.setStorage({
        key: 'uu_id',
        data: uu_id,
      })
    }
    var e = { uid: uid };
    that.requests(e);
    that.setData({
      zh: true,
      zx: false,
      zr: false,
      sort_text: '综合排序',
      shopname: options.shopname,
      uid: uid
    })
  
  },
  // 搜索
  contentInput: function (re) {
    // console.log(re.detail.value)
    var that = this
    if (re.detail.value) {
      that.setData({
        keyword: re.detail.value,
        search_show: true
      })
    } else {
      that.setData({
        search_show: false
      })
    }

  },
  bindViewTap: function () {
    var that = this
    var e = { act: that.data.act, actorder: that.data.actorder, keywords: that.data.keyword,uid: that.data.uid };
    that.requests(e);

  },
  search_clear: function () {
    this.setData({
      keyword: '',
      search_show: false

    })
  },
  // 综合
  sort_drap: function () {
    var that = this
    that.setData({
      off: !that.data.off
    })
  },
  to_default: function () {
    var that = this
    var e = { uid: that.data.uid };
    that.requests(e);
    that.setData({
      zh: true,
      zx: false,
      zr: false,
      off: false,
      def: true,
      up: false,
      down: false,
      actorder: '',
      act: '',
      sort_text: '综合排序'
    })
  },
  // 价格从高到底
  to_down: function () {
    var that = this
    var e = { actorder: "DESC", act: "price", uid: that.data.uid };
    that.requests(e);
    that.setData({
      zh: true,
      zx: false,
      zr: false,
      off: false,
      def: false,
      up: false,
      down: true,
      actorder: 'DESC',
      act: 'price',
      sort_text: '价格从高到底'
    })
  },
  // 价格从低到高
  to_up: function () {
    var that = this
    var e = { actorder: " ASC", act: "price", uid: that.data.uid };
    that.requests(e);
    that.setData({
      zh: true,
      zx: false,
      zr: false,
      off: false,
      def: false,
      up: true,
      down: false,
      actorder: 'ASC',
      act: 'price',
      sort_text: '价格从低到高'
    })
  },
  //最新
  new_best: function () {
    var that = this
    var e = { actorder: " DESC", act: "uptime", uid: that.data.uid, keywords: that.data.keyword };
    that.requests(e);
    that.setData({
      zh: false,
      zx: true,
      zr: false,
      actorder: 'DESC',
      act: 'uptime'

    })
  },
  //最热
  hot_best: function () {
    var that = this
    var e = { actorder: " DESC", act: "sales", uid: that.data.uid, keywords: that.data.keyword };
    that.requests(e);
    that.setData({
      zh: false,
      zx: false,
      zr: true,
      actorder: 'DESC',
      act: 'sales'
    })
  },

  requests: function (e) {
    var that = this
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=directsellerGoodsList&typ=json',
      data: e,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      responseType: 'text',
      success: function (res) {
        
        if (res.data.status == 200){
          that.setData({
            goods_list: res.data.data.items,
            total: res.data.data.total,
            totalsize: res.data.data.totalsize,      
            is_show_time: 0,
            is_show: 0,
            firstRow: 0,
            user_id: res.data.data.user_id,
            shop_wap_qrcode: res.data.data.shop_wap_qrcode
          })
          
        }
        
      }
    })

  },

  bindhref: function () {
    app.goTop()
    this.setData({
      show: 0
    })
  },
  //一键到顶
  onPageScroll: function (e) {
    // 获取滚动条当前位置
    var that = this
    wx.getSystemInfo({
      success: function (res) {

        if (e.scrollTop + res.windowHeight > res.windowHeight) {
          console.log("yes")
          that.setData({
            show: 1
          })
        } else {
          console.log("no")
          that.setData({
            
            show: 0
          })
        }
      },
    })
  },
  //页面上拉触底事件的处理函数
  onReachBottom: function () {
   
    var that = this
    if (that.data.is_show == 1) {
      return false
    }
    that.setData({
      firstRow: that.data.firstRow + 10
    })
    
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=directsellerGoodsList&typ=json',
      header: {
        'content-type': 'application/json'
      },
      data: { act: that.data.act, actorder: that.data.actorder, keywords: that.data.keyword, firstRow: that.data.firstRow, uid: that.data.uid },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        var arr = that.data.goods_list
        if (res.data.data.items.length > 0) {

          for (var z in res.data.data.items) {
            arr.push(res.data.data.items[z])
          }
          that.setData({
            goods_list: arr,
            is_show_time: 0,
            is_show:0

          })
        } else {
          that.setData({
            is_show: 1,
            is_show_time: 0

          })
        }

      }
    })
  },
  //分享分销
  onShareAppMessage: function (res) {
    var that = this
    var shopname = that.data.shopname
    var uid = wx.getStorageSync('u')
    return {
      title: shopname,
      path: '/pages/directseller_store/directseller_store?uid=' + uid+'&uu_id='+uid,
    }
  },

})